package com.example.myproject5

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import java.io.ByteArrayOutputStream

class SecondActivity : ComponentActivity() {

    private lateinit var buttonTakePhoto: Button
    private lateinit var imageViewPhoto: ImageView
    private val REQUEST_IMAGE_CAPTURE = 1
    private var photoBitmap: Bitmap? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_second)

        buttonTakePhoto = findViewById(R.id.photo_button)
        imageViewPhoto = findViewById(R.id.imageView_photo)

        buttonTakePhoto.setOnClickListener {
            dispatchTakePictureIntent()
        }

        // 设置返回按钮事件
        val backButton = findViewById<Button>(R.id.return_button)
        backButton.setOnClickListener {
            finish()
        }

        // 获取RadioGroup组件
        val radioGroupGender = findViewById<RadioGroup>(R.id.radioGroupGender)

        // 获取CheckBox组件
        val checkBoxTravel = findViewById<CheckBox>(R.id.checkBox)
        val checkBoxReading = findViewById<CheckBox>(R.id.checkBox2)
        val checkBoxShopping = findViewById<CheckBox>(R.id.checkBox3)
        val checkBoxCoding = findViewById<CheckBox>(R.id.checkBox4)

        // 获取输入框组件
        val editTextName = findViewById<EditText>(R.id.editTextText2)
        val editTextNumber = findViewById<EditText>(R.id.editTextText5)
        val editTextPhone = findViewById<EditText>(R.id.editTextText9)
        val editTextAddress = findViewById<EditText>(R.id.editTextText10)

        findViewById<Button>(R.id.submit_button).setOnClickListener {
            // 获取选中的性别
            val selectedGenderId = radioGroupGender.checkedRadioButtonId
            val gender = if (selectedGenderId != -1) {
                findViewById<RadioButton>(selectedGenderId).text.toString()
            } else {
                "未选择"
            }

            // 获取选中的爱好
            val hobbies = mutableListOf<String>()
            if (checkBoxTravel.isChecked) hobbies.add(checkBoxTravel.text.toString())
            if (checkBoxReading.isChecked) hobbies.add(checkBoxReading.text.toString())
            if (checkBoxShopping.isChecked) hobbies.add(checkBoxShopping.text.toString())
            if (checkBoxCoding.isChecked) hobbies.add(checkBoxCoding.text.toString())

            // 获取用户输入的其他信息
            val name = editTextName.text.toString()
            val number = editTextNumber.text.toString()
            val phone = editTextPhone.text.toString()
            val address = editTextAddress.text.toString()

            // 创建返回数据
            val returnIntent = Intent()
            returnIntent.putExtra("name", name)
            returnIntent.putExtra("number", number)
            returnIntent.putExtra("phone", phone)
            returnIntent.putExtra("address", address)
            returnIntent.putExtra("sex", gender)
            returnIntent.putStringArrayListExtra("hobby", ArrayList(hobbies))

            photoBitmap?.let { bitmap ->
                val stream = ByteArrayOutputStream()
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream)
                val byteArray = stream.toByteArray()
                returnIntent.putExtra("photo_data", byteArray)
            }
            setResult(RESULT_OK, returnIntent)
            finish()
        }
    }
    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            imageBitmap?.let {
                imageViewPhoto.setImageBitmap(it)
                imageViewPhoto.background = null
            }
        }
    }

}